from pydantic import BaseModel, Field, field_validator


class CreditCardUpdateRequest(BaseModel):
    id: int = Field(..., description="信用卡ID")
    card_no: str | None = Field(None, min_length=16, max_length=19, description="信用卡号")
    belong_bank: int | None = Field(None, gt=0, description="归属银行ID")

    @field_validator("card_no")
    def validate_card_no(cls, v):
        if v is not None:
            cleaned = v.replace(" ", "").replace("-", "")
            if not cleaned.isdigit():
                raise ValueError("卡号只能包含数字")
            if len(cleaned) not in [16, 19]:
                raise ValueError("卡号长度必须为16或19位")
        return v
